﻿/*
 * waiter.js
 *
 * jQuery plugin to provide a very basic waiting overlay on top of another DOM element
 */
(function ($) {
    var settings = {
        waitingDone: null
    };

    var methods = {
        init: function (options) {
            settings = $.extend(settings, options);

            return this.each(function () {
                var $this = $(this);

                var $data = $this.data('waiter');

                if (!$data) {
                    $data = $this.data('waiter', { target: $this, overlay: null });
                }
                else { // already have an overlay so if it's visible then do nothing.
                    if ($data.overlay && $data.overlay.is(':visible'))
                        return;
                }

                // get current dimensions of object to wait over.
                var $position = $this.position();
                var width = $this.outerWidth();
                var height = $this.outerHeight();

                var overlay = $('<div class="overlay" />');
                $data.overlay = overlay;
                $this.data('waiter', $data);

                overlay.css({
                    'position': 'absolute',
                    'top': $position.top,
                    'left': $position.left,
                    'width': width + 'px',
                    'height': height + 'px',
                    'background-color': '#000',
                    'filter': 'alpha(opacity=50)',
                    '-moz-opacity': 0.5,
                    '-khtml-opacity': 0.5,
                    'opacity': 0.5,
                    'z-index': 10000
                });

                $('body').append(overlay);
            });
        },
        destroy: function () {
            var $this = $(this);

            var $data = $this.data('waiter');

            if ($data && $data.overlay)
                $data.overlay.remove();
        }
    };

    $.fn.waiter = function (method) {

        // Method calling logic
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist on waiter object.');
        }

    };

})(jQuery);